/*
 * Copyright (c) 2015 The ddp3.0 Project,
 *
 * 中鼎博华. All Rights Reserved.
 */

package a66weding.com.jiehuntong.common.utils;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Function: 基本工具类
 * @Date: 2015-10-15 下午2:28:05
 * @author zhaol
 * @version
 */
public class Utils {

	// /////////////////////////////////单位换算//////////////////////////////////

	/**
	 * @param context
	 * @param dpValue
	 * @return
	 * @description 根据手机的分辨率从 dp 的单位 转成为 px(像素)
	 * @author zhaol
	 * @update 2015-10-19 下午3:08:05
	 */
	public static int dip2px(Context context, float dpValue) {
		if(context == null){
			return 0;
		}
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);// 四舍五入
	}


	/**
	 * @param context
	 * @param pxValue
	 * @return
	 * @description 根据手机的分辨率从 px(像素) 的单位 转成为 dp
	 * @author zhaol
	 * @update 2015-10-19 下午3:08:16
	 */
	public static int px2dip(Context context, float pxValue) {
		if(context == null){
			return 0;
		}
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	// /////////////////////////////////屏幕大小（分辨率px）//////////////////////////////////

	/**
	 * @param context
	 * @return
	 * @description 获取屏幕宽度
	 * @author zhaol
	 * @update 2015-10-19 下午3:07:44
	 */
	public static int getScreenWidth(Context context) {
		WindowManager wManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wManager.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.widthPixels;
	}

	/**
	 * @param context
	 * @return
	 * @description 获取屏幕高度
	 * @author zhaol
	 * @update 2015-10-19 下午3:07:34
	 */
	public static int getScreenHeight(Context context) {
		WindowManager wManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wManager.getDefaultDisplay().getMetrics(outMetrics);
		return outMetrics.heightPixels;
	}

	// /////////////////////////////////软键盘//////////////////////////////////

	/**
	 * @param activity
	 * @description 隐藏软键盘
	 * @author zhaol
	 * @update 2015-10-19 下午3:07:16
	 */
	public static void hideSoftInputWindow(Activity activity) {
		InputMethodManager imm = (InputMethodManager) activity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		boolean isOpen = imm.isActive();// isOpen若返回true，则表示输入法打开
		if (isOpen) {
			imm.hideSoftInputFromWindow(activity.getCurrentFocus()
					.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}

	/**
	 * @param edit
	 * @description 显示软键盘
	 * @author zhaol
	 * @update 2015-10-19 下午3:07:16
	 */
	public static void showSoftInputWindow(EditText edit) {
		InputMethodManager imm = (InputMethodManager) edit.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
	}

	/**
	 * @param context
	 * @description 检查网络是否连接
	 * @author zhaol
	 * @update 2015-10-22 上午11:44:09
	 */
	public static boolean checkNet(Context context) {
		boolean isConnected = false;
		ConnectivityManager connectivity = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info != null && info.isConnected()) {
				if (info.getState() == NetworkInfo.State.CONNECTED) {
					isConnected = true;
				}
			}
		}
		return isConnected;
	}

	/**
	 * url参数转码
	 * @param map
	 * @return
     */
	public static String getUrlParameters(Map<String, Object> map) {
		StringBuffer sb = new StringBuffer();
		if (map != null & map.size() > 0) {
			for (Map.Entry<String, Object> entry : map.entrySet()) {
				sb.append("&");
				sb.append(entry.getKey());
				sb.append("=");
				sb.append(URLEncode(entry.getValue().toString()));
			}
		}
		return sb.toString();
	}

	/**
	 *
	 * @param text
	 * @return
	 * @description 编码
	 * @author zhaol
	 * @update 2015-11-3 上午10:35:07
	 */
	public static String URLEncode(String text) {
		String result = "";
		try {
			result = URLEncoder.encode(text, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/***** 正则表达式 ********/

	/**
	 * 判断是否为数字
	 *
	 * @param str
	 * @return 是数字：true 否则：false
	 */
	public static boolean isNumeric(String str) {
		for (int i = str.length(); --i >= 0;) {
			if (!Character.isDigit(str.charAt(i))) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 判断是否为网址
	 *
	 * @param url
	 * @return
	 */
	public static boolean isHttp(String url) {
		return url.contains("http") || url.contains("https");
	}

	/**
	 *
	 * @param photo
	 * @return
	 * @description 判断手机格式
	 * @author zhaol
	 * @update 2015-12-2 下午2:57:10
	 */
	public static boolean isPhone(String photo) {
		Pattern p = Pattern
				// .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
				.compile("^(1[0-9])\\d{9}$");// 十一位，以1开头
		Matcher m = p.matcher(photo);
		return m.matches();
	}

	/**
	 *
	 * @param email
	 * @return
	 * @description 判断邮箱格式
	 * @author zhaol
	 * @update 2015-12-2 下午2:57:50
	 */
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);
		return m.matches();
	}

}
